/**
 *  *2010-4-26 下午04:33:43  add 
 */
package com.base.myproject.client.busi.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.print.Printer;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.tools.CommandSyncAssociatorDataTime;
import com.base.myproject.client.tools.CommandSyncAssociatorTimes;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.Debug;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;

/**
 * 按次数，时间限制等方式接受会员服务，多用于会所，健身房
 * 
 * @author Administrator 2010-4-26 javazgw@gmail.com
 */
public class ServiceListing3 extends BaseJDBCPanel implements
		Listener<BaseEvent>, BaseStoreListener, Printer {

	Html html = new Html();
	public ServiceListing3() {
		init();
	}

	public void init() {
	
		busi = SystemMessage.busi[8];
		this.setWidth("1024px");
		 this.setHeight("720px");
		//this.setAutoHeight(true);

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 0, 0));
		this.add(initWeigetx(), centerData);

		registerMainStore(store);
		registerStore(store);

		this.setBusino(busi);
		setdefault();
		store.addBaseStoreListener(BaseStoreEvent.EventType.ROW_CHANGED, this);
		createDialog().show();
		card.focus();
		
	}
	private void setdefault() {

		
		store.addDefault("bcode", Env.getEnvbcode()); 

		store.addDefault("busicode", busi);
		store.addDefault("busistatus", "524288");
		store.addDefault("peoplenum", 1);
//		store.addDefault("accounts", 0);
//		store.addDefault("paid", 0);
		store.addDefault("paymode", 1);
		
		
		DateTimeFormat activeFormat = DateTimeFormat
		.getFormat("yyyy-M-d H:mm:ss");
//		store.addDefault("createdate",activeFormat.format(new Date()));
//		
//		store.addDefault("servicestartdate", activeFormat.format(new Date()));
		store.addDefault("createdate",new Date());
		store.addDefault("servicestartdate",new Date());
	}
	private Widget initWeigetx() {
		VerticalPanel vpanel = new VerticalPanel();

		vpanel.setHorizontalAlign(HorizontalAlignment.CENTER);
		vpanel.add(html);
		vpanel.add(createGrid());

		return vpanel;

	}

	BaseGrid grid = null;

	BaseGrid createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

//		ColumnConfig C1 = new ColumnConfig("busicode", "busicode", 80);
//		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("bcode", "操作员", 80);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("ccode", "ccode", 80);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("busistatus", "单据状态", 80);
		columns.add(C4);
//		ColumnConfig C5 = new ColumnConfig("peoplenum", "人数", 80);
//		columns.add(C5);
		ColumnConfig C6 = new ColumnConfig("accounts", "应付", 80);
		columns.add(C6);
		ColumnConfig C7 = new ColumnConfig("paid", "实付", 80);
		columns.add(C7);
		ColumnConfig C8 = new ColumnConfig("servicestartdate",
				"开始时间", 120);
		columns.add(C8);
//		ColumnConfig C9 = new ColumnConfig("serviceenddate", "serviceenddate",
//				80);
//		columns.add(C9);
		ColumnConfig C10 = new ColumnConfig("s_cardno", "卡号", 180);
		columns.add(C10);
//		ColumnConfig C11 = new ColumnConfig("createdate", "createdate", 80);
//		columns.add(C11);
//		ColumnConfig C12 = new ColumnConfig("paymode", "paymode", 80);
//		columns.add(C12);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid(getStore(), cm);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setHeight(400);
		grid.setAutoExpandColumn("s_cardno");
		grid.getView().setForceFit(true);
		return grid;
	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("busicode");
		type.getField("busicode").setType(String.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("ccode");
		type.getField("ccode").setType(String.class);
		type.addField("busistatus");
		type.getField("busistatus").setType(String.class);
		type.addField("peoplenum");
		type.getField("peoplenum").setType(Integer.class);
		type.addField("accounts");
		type.getField("accounts").setType(Double.class);
		type.addField("paid");
		type.getField("paid").setType(Double.class);
		type.addField("servicestartdate");
		type.getField("servicestartdate").setType(Date.class);
		type.addField("serviceenddate");
		type.getField("serviceenddate").setType(Date.class);
		type.addField("s_cardno");
		type.getField("s_cardno").setType(String.class);
		type.addField("createdate");
		type.getField("createdate").setType(Date.class);
		type.addField("paymode");
		type.getField("paymode").setType(Integer.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("s_service_po");
		info.setKeyname(new String[] { "icode" });
		info
				.setSql("select busicode,bcode,ccode,busistatus,peoplenum,accounts,paid,servicestartdate,serviceenddate,s_cardno,createdate,paymode from s_service_po where DATEDIFF(day, getdate(), createdate)=0 and bcode='"+Env.getEnvbcode()+"'");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info)
		{
			public void add(BusiObject modeldata) {
				setdefault();
				add(modeldata, true, true);

			}
		};
		return store;
	}
	 int sync = 0;
	@Override
	public void handleEvent(BaseEvent be) {
		sync++;
		try {
			if (sync == 1) {

				
				Debug.printf(be.getType(), 4);
				if (be.getType() == Events.Select) {
					
				}
				else if (be.getType() == Events.KeyUp) {
					if(be.getSource().equals(card))
					{
						if(((ComponentEvent) be).getKeyCode()==10 || ((ComponentEvent) be).getKeyCode()==13)
						{
							
						
							BusiObject bo = new BusiObject();
							bo.set("s_cardno", card.getValue());
							bo.set("accounts", new Double("1"));
							bo.set("paid", new Double("1"));
							bo.set("serviceenddate", new Date());
							
							store.add(bo, true,false);
							//store._trySave(store.getCurcowindex());
							CommandSyncContainer list = new CommandSyncContainer();
							CommandSyncAssociatorTimes commandatime = new CommandSyncAssociatorTimes();
							commandatime.setS_cardno(card.getValue());
							
							CommandSyncAssociatorDataTime commandadatatime = new CommandSyncAssociatorDataTime();
							commandadatatime.setS_cardno(card.getValue());
							
							list.add(commandatime);
							
							list.add(commandadatatime);
							
							CommandSyncsql commandsql = new CommandSyncsql();
							commandsql.getV().addAll(store._trySave(store.getCurcowindex()));
							list.add(commandsql);
							
							final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
							.create(com.base.myproject.client.tools.GreetingService.class);
							greetingService.SendCommandSync(list,
									new AsyncCallback<CommandSyncReturnObject>()
									{

										@Override
										public void onFailure(Throwable caught) {
											 MessageBox.alert("错误", "网络出现连接失败！请重新提交。", null);
											
										}

										@Override
										public void onSuccess(
												CommandSyncReturnObject result) {
											if(result.isB())
											{
											store._trySaveed(store.getCurcowindex());
											store.getAt(store.getCurcowindex()).clearBusistatus();
											Info.display("", "提交成功");
											}
											else
											{
												MessageBox.alert("错误", result.getMessage(), null);
											}
											}
								
								
									});
							
							
							
							card.clearState();
							card.setValue(null);
							card.focus();
						}
					}
				}
				else if (be.getType() == Events.Focus) {
					if(be.getSource().equals(card))
					{
						html.setHtml("<font color=\"#ccccc\">磁卡感应状态</font>");
					GWT.log("磁卡感应状态", null);
					}
				}
				else if (be.getType() == Events.Blur) {
					if(be.getSource().equals(card))
					{
						html.setHtml("<font color=\"#FF0000\">磁卡非感应状态!!!!!</font>");
					GWT.log("磁卡非感应状态", null);
					}
				}
			}
		}
		finally {
			sync--;
			
		}
		

	}

	@Override
	public void BaseStoreEventHandle(BaseStoreEvent evt) {
		// TODO Auto-generated method stub

	}

	@Override
	public void Print() {
		// TODO Auto-generated method stub

	}
	TextField<String> card ;
	public Dialog createDialog()
	{
		Dialog d = new Dialog();
		card = new TextField<String>();
		card.setWidth(200);
		card.setEmptyText("鼠标光标请放在此处进行感应");
	//	d.add(new Label("鼠标光标请放在此处进行感应"));
		d.add(card);
		
		card.addListener(Events.KeyUp, this);
		card.addListener(Events.Focus, this);
		card.addListener(Events.Blur, this);
		d.setClosable(false);
		d.setShadow(true);
		d.setBlinkModal(true);
		d.setFocusWidget(card);
		d.layout();
		d.setAutoWidth(true);
		return d;
	}

}
